package com.developtools.actions;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.impl.java.stubs.index.JavaAnnotationIndex;
import com.intellij.psi.search.GlobalSearchScope;

import java.util.Collection;

/**
 * @author liujun
 */
public class DemoAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();

        ModuleManager moduleManager = ModuleManager.getInstance(project);
        Module[] modules = moduleManager.getModules();
        for (Module module : modules) {
            String name = module.getName();
            System.out.println("module--");
            System.out.println(name);
        }
        JavaAnnotationIndex javaAnnotationIndex = JavaAnnotationIndex.getInstance();
        Collection<PsiAnnotation> controllers = javaAnnotationIndex.get("RestController", project, modules[0].getModuleScope());
        controllers.forEach(psiAnnotation -> {
            System.out.println("psiAnnotation--");

            System.out.println(psiAnnotation.getText());
            PsiElement parent = psiAnnotation.getParent();
            System.out.println("1p");
            System.out.println(parent.getText() + "--" + parent.getClass().getName());

            PsiElement parentParent = parent.getParent();
            System.out.println("2p");
            System.out.println(parentParent.getText() + "--" + parentParent.getClass().getName());

            if (parentParent instanceof PsiClass) {
                PsiClass psiClass = (PsiClass) parentParent;
                String name = psiClass.getName();
                System.out.println("class --");
                System.out.println(name);

                PsiMethod[] methods = psiClass.getMethods();

            }
        });
    }
}
